package com.example.administrator.kotlin.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.example.administrator.kotlin.R
import com.example.administrator.kotlin.adapter.VerticalAdapter
import com.example.administrator.kotlin.bean.NewsBean
import com.example.administrator.kotlin.bean.Newslist
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response

class LinearHoriActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var adatper: VerticalAdapter
    private var news = ArrayList<Newslist>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_linear_hori)
        initView()
        initData()
    }

    private fun initData() {
        var url = "http://api.tianapi.com/social/?key=861a794733dd2b057e415d822bef9586&num=10"
        OkGo.get<String>(url)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        var gson: Gson = Gson()
                        val json = gson.fromJson(response?.body().toString(), NewsBean::class.java)
                        val newslist = json.newslist
                        adatper.setNewData(newslist)
                    }

                })
    }

    private fun initView() {
        recyclerView = findViewById<RecyclerView>(R.id.recyclerview_horizonl)
        recyclerView.layoutManager = LinearLayoutManager(this@LinearHoriActivity, LinearLayoutManager.VERTICAL, false)
        adatper = VerticalAdapter(news)
        recyclerView.setHasFixedSize(true)
        recyclerView.adapter = adatper

        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val itemCount = recyclerView?.layoutManager?.itemCount
                val findFirstCompletelyVisibleItemPosition = (recyclerView?.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
                val findFirstVisibleItemPosition = (recyclerView?.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
                val findLastVisibleItemPosition = (recyclerView?.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
                val childAt = recyclerView.layoutManager.getChildAt(0)
                val top = childAt?.top
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

            }
        })
    }

}
